package com.leased.service.impl;

import com.leased.exception.DatabaseException;
import com.leased.interceptor.PageMybatisInterceptor;
import com.leased.mapper.OperateRecordInfoMapper;
import com.leased.pojo.OperateRecordInfo;
import com.leased.pojo.OperateRecordInfoExample;
import com.leased.request.operate.OperateRecordRequest;
import com.leased.service.OperateRecordService;
import com.leased.utils.Page;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created by shencx on 2017/8/10.
 */
@Service
public class OperateRecordServiceImpl implements OperateRecordService{

    Logger LOG = LoggerFactory.getLogger(OperateRecordServiceImpl.class);

    @Resource
    private OperateRecordInfoMapper operateRecordInfoMapper;

    /**
     * 添加操作记录
     * @param operateRecordInfo
     * @return
     * @throws DatabaseException
     */
    @Override
    public Integer addOperateRecordInfo(OperateRecordInfo operateRecordInfo) throws DatabaseException {
        try {
            if (operateRecordInfo == null){
                LOG.error("addOperateRecordInfo 信息为空",operateRecordInfo);
                return Integer.valueOf(0);
            }
            operateRecordInfo.setCreateTime(new Date());
            int val = operateRecordInfoMapper.insertSelective(operateRecordInfo);
            if (val > 0){
                return operateRecordInfo.getId();
            }else {
                return Integer.valueOf(0);
            }
        }catch (Throwable e) {
            LOG.error("addOperateRecordInfo 发生异常",e,operateRecordInfo);
            throw new DatabaseException(e.getMessage());
        }
    }

    /**
     * 获取操作记录列表
     * @param operateRecordRequest
     * @return
     * @throws DatabaseException
     */
    @Override
    public Page<OperateRecordInfo> fetchOperateRecordInfoList(OperateRecordRequest operateRecordRequest) throws DatabaseException {
        try {
            if (operateRecordRequest == null){
                LOG.error("fetchOperateRecordInfoList 信息为空",operateRecordRequest);
                return null;
            }
            OperateRecordInfoExample example = new OperateRecordInfoExample();
            OperateRecordInfoExample.Criteria criteria = example.createCriteria();
            criteria.andUserTypeEqualTo(operateRecordRequest.getUserType());
            criteria.andUserAccountEqualTo(operateRecordRequest.getUserAccount());
            example.setOrderByClause("create_time desc");
            PageMybatisInterceptor.startPage(operateRecordRequest.getPageNum(),operateRecordRequest.getPageSize());
            operateRecordInfoMapper.selectByExample(example);
            Page<OperateRecordInfo> page = PageMybatisInterceptor.endPage();
            return page;
        }catch (Throwable e) {
            LOG.error("fetchOperateRecordInfoList 发生异常",e,operateRecordRequest);
            throw new DatabaseException(e.getMessage());
        }
    }

    /**
     * 根据用户名和类型获取记录列表
     * @param operateRecordRequest
     * @return
     * @throws DatabaseException
     */
    @Override
    public List<OperateRecordInfo> fetchOperateRecordList(OperateRecordRequest operateRecordRequest) throws DatabaseException {
        try {
            if (operateRecordRequest == null){
                LOG.error("fetchOperateRecordList 信息为空",operateRecordRequest);
                return null;
            }
            OperateRecordInfoExample example = new OperateRecordInfoExample();
            OperateRecordInfoExample.Criteria criteria = example.createCriteria();
            criteria.andUserTypeEqualTo(operateRecordRequest.getUserType());
            criteria.andUserAccountEqualTo(operateRecordRequest.getUserAccount());
            List<OperateRecordInfo> list = operateRecordInfoMapper.selectByExample(example);
            return list;
        } catch (Throwable e) {
            LOG.error("fetchOperateRecordList 发生异常",e,operateRecordRequest);
            throw new DatabaseException(e.getMessage());
        }
    }
}
